//
//  YGStatusPictureView.swift
//  sinaWeiBoGui
//
//  Created by liyonggui on 16/5/17.
//  Copyright © 2016年 liyonggui. All rights reserved.
//

import UIKit

/// 最大列数
private let maxColumn = 3

/// imageView 之间的间距
private let imageViewMargin: CGFloat = 4

/// 图片的宽度和高度
private let imageViewWH = (UIScreen.mainScreen().bounds.width - 2 * cellMargin - (CGFloat(maxColumn) - 1) * imageViewMargin) / CGFloat(maxColumn)


/// 微博配图
class YGStatusPictureView: UIView {
    
    /// 要显示图片的URL数组
    var pictureURLs: [NSURL]? {
        didSet {
            let size = layoutImages(pictureURLs)
            // 重新更新约束
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
        }
    }
    
    /// 布局9个字控件
    private func layoutImages(pictureURLs: [NSURL]?) -> CGSize{
        // 要显示图片的数量
        let count = pictureURLs?.count ?? 0
        
        /// 3列
        var numberOfColumn = 3
        
        if count == 1 {
            numberOfColumn = 1
        } else if count == 2 || count == 4 {
            numberOfColumn = 2
        }
        // 标记
        var index = 0
        
        for imageView in imageViews {
            if index >= count {
                // 隐藏
                imageView.hidden = true
            } else {
                // 显示，并布局
                imageView.hidden = false
                // 所在列数
                let col = index % numberOfColumn
                // 所在行数
                let row = index / numberOfColumn
                
                // x
                let x = CGFloat(col) * (imageViewWH + imageViewMargin)
                // y
                let y = CGFloat(row) * (imageViewMargin + imageViewWH)
                
                // 设置frame
                imageView.frame = CGRect(x: x, y: y, width: imageViewWH, height: imageViewWH)
                
                // 设置图片
                let url = pictureURLs![index]
                imageView.setImageWithURL(url)
            }
            index++
        }
        // 计算配图的大小
        if count == 0 {
            return CGSizeZero
        }
        
        let numberOfRow = (count + numberOfColumn - 1 ) / numberOfColumn
        let width = CGFloat(numberOfColumn) * imageViewWH + (CGFloat(numberOfColumn) - 1 ) * imageViewMargin
        let height = CGFloat(numberOfRow) * imageViewWH + (CGFloat(numberOfRow) - 1) * imageViewMargin
//        let height = CGFloat(numberOfRow) * imageViewWH + (CGFloat(numberOfRow) - 1) * imageViewMargin
//        print("\(CGSize(width: width, height: height))")
        return CGSize(width: width, height: height)
    }
    
    // MARK: - 属性
    private var imageViews = [UIImageView]()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    /// 设置UI
    private func prepareUI() {
        backgroundColor = UIColor.clearColor()
        
        // 添加9个子控件
        for _ in 0..<9 {
            let imageView = UIImageView()
            imageView.backgroundColor = UIColor.randomColor()
            
            // 设置图片填充模式
            imageView.contentMode = UIViewContentMode.ScaleAspectFill
            imageView.clipsToBounds = true
            
            addSubview(imageView)
            
            // 添加到数组
            imageViews.append(imageView)
        }
        
    }
}
